<!DOCTYPE html>
<html>
<head>
    <title>Azure Storage Client Library Question</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
        .question { background: #f5f5f5; padding: 15px; border-radius: 5px; margin-bottom: 20px; }
        .code { background: #333; color: white; padding: 15px; border-radius: 5px; font-family: monospace; white-space: pre; overflow-x: auto; }
        .statement { margin: 15px 0; padding: 10px; background: #eee; border-radius: 5px; }
        .options { display: flex; gap: 15px; margin-top: 5px; }
        .option { display: flex; align-items: center; }
        button { padding: 8px 15px; background: #0078d4; color: white; border: none; border-radius: 4px; cursor: pointer; margin-top: 10px; }
        .answer { display: none; margin-top: 20px; padding: 15px; background: #e6f2ff; border-radius: 5px; }
        .correct { color: green; font-weight: bold; }
        .explanation { margin-top: 10px; }
    </style>
</head>
<body>
    <div class="question">
        <h3>QUESTION NO: 218 HOTSPOT</h3>
        <p>You are developing a solution that uses the Azure Storage Client library for .NET. You have the following code:</p>
        
        <div class="code">
01 CloudBlockBlob src = null;
02 try
03 {
04     src = container.ListBlobs().OfType&lt;CloudBlockBlob&gt;().FirstOrDefault();
05     await src.AcquireLeaseAsync(null);
06     var dat = container.GetBlockBlobReference(src.Name);
07     string idcpi = await dat.StartCopyAsync(src);
08     await dat.FetchAttributesAsync();
09     return idcpi;
10 }
11 catch (Exception e)
12 {
13     throw;
14 }
15 finally
16 {
17     if (src != null)
18         await src.BreakLeaseAsync(new TimeSpan(0));
19 }
        </div>
        
        <p>For each of the following statements, select Yes if the statement is true. Otherwise, select No.</p>
        <p><strong>NOTE:</strong> Each correct selection is worth one point.</p>
        
        <div class="statement">
            <p>1. The code creates an infinite lease</p>
            <div class="options">
                <div class="option">
                    <input type="radio" id="statement1-yes" name="statement1" value="yes">
                    <label for="statement1-yes">Yes</label>
                </div>
                <div class="option">
                    <input type="radio" id="statement1-no" name="statement1" value="no">
                    <label for="statement1-no">No</label>
                </div>
            </div>
        </div>
        
        <div class="statement">
            <p>2. The code at line 06 always creates a new blob</p>
            <div class="options">
                <div class="option">
                    <input type="radio" id="statement2-yes" name="statement2" value="yes">
                    <label for="statement2-yes">Yes</label>
                </div>
                <div class="option">
                    <input type="radio" id="statement2-no" name="statement2" value="no">
                    <label for="statement2-no">No</label>
                </div>
            </div>
        </div>
        
        <div class="statement">
            <p>3. The finally block releases the lease</p>
            <div class="options">
                <div class="option">
                    <input type="radio" id="statement3-yes" name="statement3" value="yes">
                    <label for="statement3-yes">Yes</label>
                </div>
                <div class="option">
                    <input type="radio" id="statement3-no" name="statement3" value="no">
                    <label for="statement3-no">No</label>
                </div>
            </div>
        </div>
        
        <button onclick="showAnswer()">查看答案</button>
        
        <div id="answer" class="answer">
            <h3>正确答案：</h3>
            
            <div class="statement">
                <p>1. The code creates an infinite lease</p>
                <p><span class="correct">Yes</span></p>
                <div class="explanation">
                    <p>Line 05: <code>await src.AcquireLeaseAsync(null)</code> creates an infinite lease when no duration is specified.</p>
                </div>
            </div>
            
            <div class="statement">
                <p>2. The code at line 06 always creates a new blob</p>
                <p><span class="correct">No</span></p>
                <div class="explanation">
                    <p>Line 06: <code>GetBlockBlobReference()</code> only creates a reference object, not a new blob. The blob is only created when data is written to it.</p>
                </div>
            </div>
            
            <div class="statement">
                <p>3. The finally block releases the lease</p>
                <p><span class="correct">No</span></p>
                <div class="explanation">
                    <p>Line 18: <code>BreakLeaseAsync()</code> breaks the lease but doesn't release it. To release a lease, you need to call <code>ReleaseLeaseAsync()</code> with the lease ID.</p>
                </div>
            </div>
            
            <h3>代码解析：</h3>
            <ol>
                <li><strong>租约管理</strong>：
                    <ul>
                        <li><code>AcquireLeaseAsync(null)</code>：获取无限期租约（默认60秒续期）</li>
                        <li><code>BreakLeaseAsync(TimeSpan.Zero)</code>：立即中断租约（非释放）</li>
                    </ul>
                </li>
                <li><strong>Blob操作</strong>：
                    <ul>
                        <li><code>GetBlockBlobReference()</code>：仅创建内存引用对象</li>
                        <li><code>StartCopyAsync()</code>：启动异步复制时才实际操作存储</li>
                    </ul>
                </li>
                <li><strong>异常处理</strong>：
                    <ul>
                        <li>finally块确保租约被中断（即使复制失败）</li>
                        <li>中断后其他客户端需等待租约过期才能操作</li>
                    </ul>
                </li>
            </ol>
        </div>
    </div>

    <script>
        function showAnswer() {
            document.getElementById('answer').style.display = 'block';
            
            // Display user selections
            const statements = [
                { name: 'statement1', text: 'The code creates an infinite lease' },
                { name: 'statement2', text: 'The code at line 06 always creates a new blob' },
                { name: 'statement3', text: 'The finally block releases the lease' }
            ];
            
            const userSelections = document.createElement('div');
            userSelections.innerHTML = '<h3>您的选择：</h3>';
            
            statements.forEach(stmt => {
                const selected = document.querySelector(`input[name="${stmt.name}"]:checked`);
                if (selected) {
                    userSelections.innerHTML += `<p>${stmt.text}: ${selected.value === 'yes' ? 'Yes' : 'No'}</p>`;
                }
            });
            
            document.getElementById('answer').prepend(userSelections);
        }
    </script>
</body>
</html>
